package com.shundr.shipper.common;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shundr.shipper.R;
import com.shundr.shipper.base.BaseActivity;
import com.shundr.shipper.common.adapter.AreaAdapter;
import com.shundr.shipper.common.dao.AreaDao;
import com.shundr.shipper.common.dao.HistoryAreaDao;
import com.shundr.shipper.common.model.AreaInfo;
import com.shundr.shipper.common.model.HistoryAreaInfo;
import com.shundr.shipper.common.util.DateUtil;
import com.shundr.shipper.common.util.JsonUtil;
import com.shundr.shipper.common.util.ToastHelper;
import com.shundr.shipper.frame.constant.LContext;
import com.shundr.shipper.frame.util.LLogger;
import com.shundr.shipper.frame.util.StringUtil;

/**
 * 单选城市加历史
 * AreaGridActivity
 * 
 * @author larry 2015-7-21 上午10:04:46 
 * @version 1.0.0
 *
 */
public class AreaGridActivity extends BaseActivity {
	
	private LinearLayout mLayoutHistory;
	
	private Button btnBack;
	private TextView tvName;
	private Button btnConfirm;
	
	private GridView mGridView;
	
	private List<AreaInfo> mAreaList = new ArrayList<AreaInfo>();
	private AreaDao mDao;
	private HistoryAreaDao mHistoryDao;
	
	private List<AreaInfo> mHistoryList = new ArrayList<AreaInfo>();
	private AreaAdapter mHistoryAdapter;
	
	private AreaAdapter mAdapter;
	
	private List<AreaInfo> selectedList = new ArrayList<AreaInfo>();
	private AreaInfo mInfo = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_area_grid);
		mDao = new AreaDao();
		mHistoryDao = new HistoryAreaDao();
		String title = getIntent().getStringExtra("title");
		if(!StringUtil.isNull(title)){
			setTitle(title);
		}
		initView();
		initData();
	}
	
	private void initData(){
		String code = getIntent().getStringExtra("code");
		if(StringUtil.isNull(code)){
			mInfo = mDao.getAreaByCode(LContext.nullStr);
		}else{
			mInfo = mDao.getAreaByParentCode(code);
			if(StringUtil.isNull(mInfo.getCode())){
				btnBack.setVisibility(View.GONE);
			}else{
				btnBack.setVisibility(View.VISIBLE);
			}
			tvName.setText(mInfo.getName());
		}
		
		tvName.setText(mInfo.getName());
		
		initGridView();
		
		List<HistoryAreaInfo> histories = mHistoryDao.getHistoryAreas();
		if(histories.size() > 0){
			mLayoutHistory.setVisibility(View.VISIBLE);
		}else{
			return;
		}
		for(HistoryAreaInfo info : histories){
			mHistoryList.add(new AreaInfo(info.getName(), info.getCode()));
		}
		mHistoryAdapter.notifyDataSetChanged();
	}
	
	private void initView(){
		mLayoutHistory = (LinearLayout)findViewById(R.id.layout_1);
		
		mHistoryAdapter = new AreaAdapter(mContext, mHistoryList);
		GridView gvHistory = (GridView) findViewById(R.id.grid_history);
		gvHistory.setAdapter(mHistoryAdapter);
		gvHistory.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				List<AreaInfo> list = new ArrayList<AreaInfo>();
				list.add(mHistoryList.get(position));
				updateHistory(mHistoryList.get(position));
				Intent intent = new Intent();
				intent.putExtra("json", JsonUtil.toJson(list).toString());
				setResult(502,intent);
				finish();
			}
		});
		
		btnBack = (Button)findViewById(R.id.btn_back);
		tvName = (TextView)findViewById(R.id.tv_name);
		btnConfirm = (Button)findViewById(R.id.btn_confim);
		btnBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mInfo = mDao.getAreaByParentCode(mInfo.getParentCode());
				if(StringUtil.isNull(mInfo.getCode())){
					btnBack.setVisibility(View.GONE);
				}else{
					btnBack.setVisibility(View.VISIBLE);
				}
				tvName.setText(mInfo.getName());
				if(StringUtil.isNull(mInfo.getCode())){
					mAreaList.clear();
					mAreaList.addAll(mDao.getAreasByParent("00"));
					mAdapter.notifyDataSetChanged();
				}else{
					initGridView();
				}
			}
		});
		
		btnConfirm.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (mInfo == null) {
					ToastHelper.toastShort(mContext, "请先选择一个地址");
				} else {
					selectedList.add(mInfo);
					updateHistory(mInfo);
					Intent intent = new Intent();
					String json = JsonUtil.toJson(selectedList).toString();
					LLogger.e(json);
					intent.putExtra("json", json);
					setResult(502,intent);
					finish();
				}
			}
		});
		
		mAdapter = new AreaAdapter(mContext, mAreaList);
		mGridView = (GridView) findViewById(R.id.gridview);
		mGridView.setAdapter(mAdapter);
		
		mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				mInfo = mAreaList.get(position);
				if(StringUtil.isNull(mInfo.getCode())){
					btnBack.setVisibility(View.GONE);
				}else{
					btnBack.setVisibility(View.VISIBLE);
				}
				tvName.setText(mInfo.getName());
				if(!StringUtil.isNull(mInfo.getCode())){
					initGridView();
				}
			}
		});
	}
	
	private void initGridView(){
		List<AreaInfo> list = new ArrayList<AreaInfo>();
		if(mInfo == null ||  StringUtil.isNull(mInfo.getCode())){
			list.addAll(mDao.getAreasByParent("00"));
		}else{
			list.addAll(mDao.getAreasByParent(mInfo.getCode()));
		}
		if(list.size() > 0){
			mAreaList.clear();
			mAreaList.addAll(list);
			mAdapter.notifyDataSetChanged();
		}
	}
	
	private void updateHistory(AreaInfo info){
		HistoryAreaInfo history = new HistoryAreaInfo(info.getName(), info
				.getCode(), info.getParentCode(), info.getAreaCode());
		history.setTime(DateUtil.getNowTime());
		mHistoryDao.insert(history);
	}

}
